//
//  WebView_ViewController.m
//  
//
//  Created by MMC on 13-10-8.
//
//

#import "WebView_ViewController.h"
#import "GlobalVar.h"
@interface WebView_ViewController ()

@property(nonatomic,retain)IBOutlet UITextView *Siliwen_Text;
@property(nonatomic,retain)IBOutlet UIView *TuiJian_View;
@property(nonatomic,retain)IBOutlet UITextField *TuiJian_Text;
@end

@implementation WebView_ViewController
@synthesize Siliwen_Text,TuiJian_Text;
@synthesize Content,TuiJian_View;
@synthesize ShowTuiJian_Btn;

-(void)dealloc{
    self.TuiJian_Text = nil;
    self.TuiJian_View = nil;
    self.Siliwen_Text = nil;
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    NSLog(@"====WebView_ViewController==init=====");
    [super viewDidLoad];
    self.Siliwen_Text.layer.cornerRadius = 10;
    [self.Siliwen_Text setText:Content];
    if(ShowTuiJian_Btn)
    {
        UIBarButtonItem *rightButton =[[[UIBarButtonItem alloc]initWithTitle:@"现在推荐"
                                                                       style:UIBarButtonItemStyleDone
                                                                      target:self action:@selector(ShowTuiJianView)]autorelease];
        self.navigationItem.rightBarButtonItem = rightButton;
    }
}
-(IBAction)CancelSMS:(id)sender
{
    [(UIView *)[self.view viewWithTag:8890] removeFromSuperview];
    [self.TuiJian_View removeFromSuperview];
}

-(IBAction)SendSMS:(id)sender
{
    if([self.TuiJian_Text.text length] > 0)
    {
        [(UIView *)[self.view viewWithTag:8890] removeFromSuperview];
        [self.TuiJian_View removeFromSuperview];
        [self showMessageView:self.TuiJian_Text.text];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"请输入好友的手机号码" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确  定", nil];
        [alert show];
        alert = nil;
    }
}

-(void)ShowTuiJianView
{
    if(![[NSUserDefaults standardUserDefaults] objectForKey:@"LOGIN_USER_NUMBER"])
    {
        UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"提醒" message:@"请先绑定账户在操作." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确  定", nil];
        [alert show];
        [alert release];
        alert = nil;
        return;
    }
    UIView *mask = [[[UIView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.bounds]autorelease];
    [mask setBackgroundColor:[UIColor blackColor]];
    [mask setTag:8890];
    [mask setAlpha:0.7f];
    [self.view addSubview:mask];
    self.TuiJian_View.layer.cornerRadius = 10;
    self.TuiJian_View.layer.masksToBounds = YES;
    CGRect frame = self.TuiJian_View.frame;
    frame.origin.x = 160 - frame.size.width/2;
    frame.origin.y = 100;
    [self.TuiJian_View setFrame:frame];
    [self.view addSubview:self.TuiJian_View];
    [self.TuiJian_Text becomeFirstResponder];
}

- (void)showMessageView:(NSString *)tel
{
    if( [MFMessageComposeViewController canSendText] )
    {
        MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc]init]; //autorelease];
        controller.recipients = [NSArray arrayWithObject:tel];
        NSString *Account = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"LOGIN_USER_KEY"]];
        NSString *SMSBODY=@"";
        if ([[NSUserDefaults standardUserDefaults]objectForKey:@"tuijianContent"]) {
            SMSBODY=[NSString stringWithString:[[NSUserDefaults standardUserDefaults]objectForKey:@"tuijianContent"]];
        }else
        {
            SMSBODY = [NSString stringWithFormat:@"我正在用%@,免费送6-60分钟试%@",SOFT_NAME,DOWNLOAD_ADDRESS];
        }
        controller.body = SMSBODY;
        controller.messageComposeDelegate = self;
        [self presentModalViewController:controller animated:YES];
        [[[[controller viewControllers] lastObject] navigationItem] setTitle:@"推荐好友"];//修改短信界面标题
        NSString* preMD5Str=[NSString stringWithFormat:@"%@%@%@",Account,SOFT_ID,@"ysw"];
        NSString* md5Str=[GlobalVar md5Str:preMD5Str];
        ASIFormDataRequest* request;
        if ([GlobalVar sharedInstance].blnUseProxy==YES)
        {
            request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",origin_url_prefix,@"recom.php"]]];
            [request setProxyPort:ProxyPort];
            [request setProxyHost:[GlobalVar getTheBestIp]];//
        }else
        {
            request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", [GlobalVar getTheBestUrl],@"proxy.php"]]];
            [request setPostValue:origin_url_prefix forKey:@"apiurl"];
            [request setPostValue:@"recom" forKey:@"act"];
        }
        [request setDelegate:self];
        [request setTimeOutSeconds:TIME_OUT_SECONDS];
        [request setPostValue:SOFT_ID forKey:@"softid"];
        [request setPostValue:Account forKey:@"mobile"];
        [request setPostValue:tel forKey:@"friend"];
        [request setPostValue:md5Str forKey:@"md5"];
        [request setPostValue:MULTIAGENTSWITCH forKey:@"multiAgent"];
        [request setPostValue:NETWORKSTATE forKey:@"apn"];
        [request setPostValue:SOFT_VER forKey:@"version"];
        [request setDidFinishSelector:@selector(tuijianSuccess:)];
        [request setDidFailSelector:@selector(tuijianFail:)];
        [request setShouldAttemptPersistentConnection:NO];
        [request startAsynchronous];
    }else{
        
        [self alertWithTitle:@"提示信息" msg:@"设备没有短信功能"];
    }
}

-(void)tuijianSuccess:(ASIHTTPRequest*)request
{
    NSLog(@"推荐结果:%@",[request responseString]);
}


-(void)tuijianFail:(ASIHTTPRequest*)request
{
    NSLog(@"fail:%@",[request error]);
}



//MFMessageComposeViewControllerDelegate

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    
    [controller dismissModalViewControllerAnimated:NO];//关键的一句   不能为YES
    switch ( result )
    {
        case MessageComposeResultCancelled:
            [self alertWithTitle:@"提示信息" msg:@"发送取消"];
            break;
        case MessageComposeResultFailed:// send failed
            [self alertWithTitle:@"提示信息" msg:@"发送失败"];
            break;
        case MessageComposeResultSent:
            [self alertWithTitle:@"提示信息" msg:@"发送成功"];
            break;
        default:
            break;
    }
}


- (void) alertWithTitle:(NSString *)title msg:(NSString *)msg
{
    
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:msg
                                                   delegate:self
                                          cancelButtonTitle:nil
                                          otherButtonTitles:@"确定", nil];
    
    [alert show];  
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
